package baseproject.com.mybaseproject.ui.base

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import baseproject.com.mybaseproject.mvp.contract.IContract
import baseproject.com.mybaseproject.utils.ActivityUtils
import baseproject.com.mybaseproject.utils.ToastUtils
import com.gyf.barlibrary.ImmersionBar
import com.tbruyelle.rxpermissions2.RxPermissions
import com.xiasuhuei321.loadingdialog.view.LoadingDialog

abstract class BaseActivity : AppCompatActivity(), IContract.IBaseView {

    open var mActivity: AppCompatActivity? = null
    open lateinit var rxPermissions: RxPermissions
    open lateinit var mImmersionBar : ImmersionBar
    open  var ld : LoadingDialog ?= null

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        getLayoutId()?.let { setContentView(it) }
        //初始化沉浸式
        if (isImmersionBarEnabled()){
            initImmersionBar()
        }
        mActivity = this
        //入栈
        ActivityUtils.pushActivity(this)
        rxPermissions = RxPermissions(this)
    }

    open fun initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this)
        mImmersionBar
                ?.statusBarDarkFont(true, 0.2f)
                ?.init()
    }
    /**
     * 是否在Activity使用沉浸式  默认使用
     *
     * @return the boolean
     */
    open fun isImmersionBarEnabled(): Boolean {
        return true
    }

    /**
     * 获取子布局
     */
    abstract fun getLayoutId(): Int?

    override fun showToast(msg: String?) {
        if (msg != null){
            ToastUtils.showToast(msg)
        }
    }

    override fun showToast(msg: Int) {
        ToastUtils.showToast(msg)
    }

    /**
     * 显示加载条
     */
    override fun showProgress(msg: String) {
        ld = LoadingDialog(this)
        ld?.setInterceptBack(false)
                ?.setLoadingText(msg)
                ?.show()
    }

    /**
     * 隐藏加载条
     */
    override fun hideProgress() {
        ld?.close()
    }

    override fun onDestroy() {
        super.onDestroy()
        //销毁沉浸式
        mImmersionBar?.destroy()
        //出栈
        ActivityUtils.removeActivity(this)
    }
}